<template>
    <view class="MGeneralField">{{curValue}}</view>
</template>

<script>

import MForm from '../js/MForm'
import FormBase from "../js/FormBase";
import dayjs from "dayjs";
export default {
    name: "MGeneralField",
    mixins: [MForm,FormBase],
    data() {
        return {
            curValue: "",
        }
    },
    methods: {
        //初始化
        init(){
            let fieldType =this.metadata.options.fieldType || 'CREATE_BY_NAME_' ;
            let userInfo=this.data["USER_INFO_"]?JSON.parse(this.data["USER_INFO_"]) : {};
            userInfo["CREATE_TIME_"]=this.data.CREATE_TIME_ || "";
            userInfo["UPDATE_TIME_"]=this.data.UPDATE_TIME_ || "";

            if(userInfo){
                if (this.metadata.options.showType=='custom'){
                    let fieldResult=this.metadata.options.fieldResult;
                    if(fieldResult){
                        let reg=/\$\{(.*?)\}/mg;
                        fieldResult=fieldResult.replace(reg,function (arg1,arg2){
                            let key=arg2.trim();
                            return userInfo[key] || "";
                        });
                        this.curValue=fieldResult
                    }else {
                        this.curValue="";
                    }
                }else {
                    this.curValue=this.buildValue(fieldType,userInfo)
                }
            }else {
                this.curValue="";
            }
        },
        //处理并返回数据
        buildValue(fieldType,userInfo){
            var curValue=""
            if(fieldType=="CREATE_TIME_" || fieldType=="UPDATE_TIME_"){
                curValue=this.data[fieldType]?dayjs(this.data[fieldType]).format('YYYY-MM-DD HH:mm:ss'):"";
            } else {
                curValue=userInfo[fieldType] || "";
            }
            return curValue;
        }
    },
    watch: {
        data:{
            handler:function (){
                this.init();
            },
            deep:true,
            immediate:true
        }
    },
}
</script>

<style scoped>
.MGeneralField {
    background: rgba(245, 246, 249, 1);
    height: 2rem;
    display: flex;
    align-items: center;
    padding: 0 0.5rem;
    border-radius: 4px;
    border: none;
}
</style>
